package com.heima.admin.controller.v1;


import com.heima.admin.service.AdChannelService;
import com.heima.apis.admin.AdChannelControllerApi;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 频道信息表 前端控制器
 * </p>
 *
 * @author liulei
 * @since 2021-01-07
 */
@RestController
@RequestMapping("/api/v1/channel")
@Api(tags = "频道管理")
public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    private AdChannelService channelService;

    @Override
    @PostMapping("/list")
    @ApiOperation(value = "分页查询频道列表")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto dto) {
        return channelService.findByNameAndPage(dto);
    }

    /**
     * 新增频道
     *
     * @param channel
     * @return
     */
    @Override
    @PostMapping("/save")
    @ApiOperation(value = "新增频道列表")
    public ResponseResult insert(@RequestBody AdChannel channel) {
        return channelService.insert(channel);
    }

    /**
     * 修改
     * @param adChannel
     * @return
     */
    @Override
    @PostMapping("/update")
    @ApiOperation(value = "修改频道列表")
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        return channelService.update(adChannel);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/del/{id}")
    @ApiOperation(value = "删除频道列表")
    public ResponseResult deleteById(@PathVariable("id") Integer id) {
        /*if(true){
            throw new CustomException(ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"自定义异常"));
        }*/
        return channelService.deleteById(id);
    }

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }
}
